package co.fitstart.entity.credit.product;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.coupon.CreditCouponBatch;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "credit_product")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class CreditProduct {
	
	@Id
	@GeneratedValue
	private Long id;
	
	/** 积分商品名称 */
	private String name;
	
	/**
	 * CreditProductTag's alias
	 * HOT,NEW
	 */
	private String tags;
	
	/** 积分商品封面 */
	private String cover;
	
	/** 积分商品简介 */
	private String summary;
	
	/** 积分商品所需积分 */
	private BigDecimal price;
	
	/** 积分商品所需积分 (当前) */
	private BigDecimal currentPrice;
	
	/** 积分商品销售数量 */
	private int salesAmount;
	
	/** 产品类型 */
	@Enumerated(value = EnumType.STRING)
	private CreditProductType type;
	
	/** 权重 */
	@JSONField(serialize = false)
    private int weight;
	
	/** 积分商品数量 */
	private Integer amount;
	
	/** 所属积分商品券 */
	private Long creditCouponBatchId;
	
	private Date createTime;
	
	private Date modifyTime;
	
	private Long operator;
	
	@Column(name = "is_buyable")
	private boolean buyable;
	
	@Column(name = "is_disabled")
	private boolean disabled;
	
	/**
	 * The default constructor
	 */
	public CreditProduct() {
	}
	
	/**
	 * 
	 * @param tags
	 * @return
	 */
	public String[] tag (String tags) {
		return tags.split(",");
	}
	
	/**
	 * 
	 * @param tags
	 * @return
	 */
	public CreditProduct tags(List<String> tags) {
		this.tags = StringUtils.join(tags, ',');
		return this;
	}
	
	/**
	 * 
	 * @return
	 */
	public CreditProduct modify() {
		this.modifyTime = new Date();
		return this;
	}
	
	/**
	 * 
	 * @param operator
	 * @return
	 */
	public CreditProduct initialize(Long operator) {
		this.createTime = new Date();
		this.modifyTime = new Date();
		this.type = CreditProductType.PRODUCT;
		this.operator = operator;
		this.weight = 0;
        this.disabled = false;
        this.buyable = true;
        return this;
	}
	
	/**
	 * 
	 * @param operator
	 * @param creditCouponBatch
	 * @return
	 */
	public CreditProduct initCoupon(Long operator, CreditCouponBatch creditCouponBatch) {
		this.createTime = new Date();
		this.modifyTime = new Date();
		this.operator = operator;
		this.weight = 0;
        this.disabled = false;
        this.price = new BigDecimal(creditCouponBatch.getCreditValue());
        this.type = CreditProductType.COUPON;
        this.amount = creditCouponBatch.getAmount();
        this.buyable = true;
        return this;
	}
	
	/**
	 * @param amount
	 * @param creditProduct 
	 */
	public CreditProduct exchange(Integer amount) {
		
		this.amount = this.amount - amount;
		this.salesAmount = this.salesAmount + amount;
		
		return this;
	}
	
    /**
     * display or hide product
     *
     * @return
     */
    public CreditProduct editDisabled() {
        this.disabled = this.disabled ? false : true;
        this.modifyTime = new Date();
        return this;
    }
    
    /**
     * edit buyable status
     *
     * @return
     */
    public CreditProduct editBuyable() {
        this.buyable = this.buyable ? false : true;
        this.modifyTime = new Date();
        return this;
    }
    
    /**
     * edit cover
     *
     * @param cover
     * @return
     */
    public CreditProduct editCover(String cover) {
        this.cover = cover;
        this.modifyTime = new Date();
        return this;
    }
    
    /**
     * Down serial
     *
     * @return
     */
    public CreditProduct unstick() {
        this.weight = 100;
        this.modifyTime = new Date();
        return this;
    }

    /**
     *
     * @param weight
     * @return
     */
    public CreditProduct stick(int weight) {
        this.weight = weight + 1;
        this.modifyTime = new Date();
        return this;
    }
    
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the tags
	 */
	public String getTags() {
		return tags;
	}

	/**
	 * @return the cover
	 */
	public String getCover() {
		return cover;
	}

	/**
	 * @return the summary
	 */
	public String getSummary() {
		return summary;
	}

	/**
	 * @return the price
	 */
	public BigDecimal getPrice() {
		return price;
	}

	/**
	 * @return the currentPrice
	 */
	public BigDecimal getCurrentPrice() {
		return currentPrice;
	}

	/**
	 * @return the salesAmount
	 */
	public int getSalesAmount() {
		return salesAmount;
	}

	/**
	 * @return the type
	 */
	public CreditProductType getType() {
		return type;
	}

	/**
	 * @return the weight
	 */
	public int getWeight() {
		return weight;
	}

	/**
	 * @return the amount
	 */
	public Integer getAmount() {
		return amount;
	}

	/**
	 * @return the creditCouponBatchId
	 */
	public Long getCreditCouponBatchId() {
		return creditCouponBatchId;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @return the modifyTime
	 */
	public Date getModifyTime() {
		return modifyTime;
	}

	/**
	 * @return the operator
	 */
	public Long getOperator() {
		return operator;
	}

	/**
	 * @return the buyable
	 */
	public boolean isBuyable() {
		return buyable;
	}

	/**
	 * @return the disabled
	 */
	public boolean isDisabled() {
		return disabled;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param tags the tags to set
	 */
	public void setTags(String tags) {
		this.tags = tags;
	}

	/**
	 * @param cover the cover to set
	 */
	public void setCover(String cover) {
		this.cover = cover;
	}

	/**
	 * @param summary the summary to set
	 */
	public void setSummary(String summary) {
		this.summary = summary;
	}

	/**
	 * @param price the price to set
	 */
	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	/**
	 * @param currentPrice the currentPrice to set
	 */
	public void setCurrentPrice(BigDecimal currentPrice) {
		this.currentPrice = currentPrice;
	}

	/**
	 * @param salesAmount the salesAmount to set
	 */
	public void setSalesAmount(int salesAmount) {
		this.salesAmount = salesAmount;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(CreditProductType type) {
		this.type = type;
	}

	/**
	 * @param weight the weight to set
	 */
	public void setWeight(int weight) {
		this.weight = weight;
	}

	/**
	 * @param amount the amount to set
	 */
	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	/**
	 * @param creditCouponBatchId the creditCouponBatchId to set
	 */
	public void setCreditCouponBatchId(Long creditCouponBatchId) {
		this.creditCouponBatchId = creditCouponBatchId;
	}

	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * @param modifyTime the modifyTime to set
	 */
	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	/**
	 * @param operator the operator to set
	 */
	public void setOperator(Long operator) {
		this.operator = operator;
	}

	/**
	 * @param buyable the buyable to set
	 */
	public void setBuyable(boolean buyable) {
		this.buyable = buyable;
	}

	/**
	 * @param disabled the disabled to set
	 */
	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

}
